package com.heima.sc.order.controller;

import com.heima.sc.order.service.OrderService;
import com.itheima.sc.pojo.Order;
import com.itheima.sc.response.UserOrderDTO;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/findOrder")
    public List<Order> findOrder(@RequestParam("username")String username){
        return orderService.findOrderByUserName(username);
    }
    @Autowired
    private Mapper dozerMapper;
    @GetMapping("/findOrderDTO")
    public List<UserOrderDTO> findOrderDTO(@RequestParam("username")String username){
        List<Order> orderList = orderService.findOrderByUserName(username);
        List<UserOrderDTO> list=new ArrayList<>();
        for (Order order : orderList) {
            UserOrderDTO userOrderDTO = dozerMapper.map(order, UserOrderDTO.class);
            list.add(userOrderDTO);
        }
        return list;
    }
}
